---@class UIHeroSelect : UIBase
UIHeroSelect = UIBase.New(PanelNames.UIHeroSelect)

function UIHeroSelect:OnCreate()
    self.btnBack = self:GetChild("btnBack")
    self.btnDeck = self:GetChild("btnDeck")
    self.itemList = self:GetList("list")
    self.heroList = {}
    ---@type HeroBigIcon[]
    self.selectedItemList = {}
    self.selectedList = {}
    for i = 0,7 do
        ---@type HeroBigIcon
        local item = self:GetChild("hero"..i)
        item:SetIndex(i+1)
        item:RegisterOnClick(self.ClickSelectedItem,self)
        table.insert(self.selectedItemList,item)
    end
    self:RegisterOnClick(self.btnBack,self.ClickBack)
    self:RegisterOnClick(self.btnDeck,self.ClickDeck)
    self:SetItemRenderer(self.itemList,self.ItemRender)
    self:RegisterEvent(UIEvent.FreshHeroSelect, self.Refresh)
    self:Refresh()
end
function UIHeroSelect:OnDestroy()

end
function UIHeroSelect:Refresh()
    local useDeckIndex = decodeJsonStrToTable(GetPlayerPrefs("SelectedDeckIndex")) or 1
    local tempList = decodeJsonStrToTable(GetPlayerPrefs("SelectedHeroList"..useDeckIndex))
    self.selectedList = {}
    --local tempList = decodeJsonStrToTable(GetPlayerPrefs("SelectedCardList"))
    if tempList and tempList ~= "" then
        for k,v in pairs(tempList) do
            if DataTable.GetById("t_hero",v) then
                table.insert(self.selectedList,v)
            end
        end
    else
        tempList = decodeJsonStrToTable(GetPlayerPrefs("SelectedCardList"))
        if tempList and tempList ~= "" then
            for k,v in pairs(tempList) do
                if DataTable.GetById("t_hero",v) then
                    table.insert(self.selectedList,v)
                end
            end
        end
    end
    self.heroList = {}
    local cfg = DataTable.Get("t_hero")
    for k,v in pairs(cfg) do
        if v.f_CompanyType == Battle.ActorType.Hero then
            table.insert(self.heroList,v.f_ID)
        end
    end
    table.sort(self.heroList)
    self.itemList.numItems = #self.heroList
    self:RefreshSelectedItem()
end
function UIHeroSelect:RefreshSelectedItem()
    for k,v in pairs(self.selectedItemList) do
        local id = self.selectedList[k]
        if id then
            v:SetHeroId(id)
        else
            v:SetEmpty()
        end
    end
end
function UIHeroSelect:IsSelect(heroId)
    return table.ContainsValue(self.selectedList,heroId)
end
function UIHeroSelect:SetSelect(heroId,isSelect)
    if isSelect then
        table.insert(self.selectedList,heroId)
    else
        table.RemoveValue(self.selectedList,heroId,true)
    end
end
---@param go HeroIcon
function UIHeroSelect:ItemRender(index,go)
    local id = self.heroList[index]
    go:SetIndex(index)
    go:SetSelected(self:IsSelect(id))
    go:RegisterOnClick(self.ClickItem,self)
    go:SetHeroId(id)
end
---@param go HeroIcon
function UIHeroSelect:ClickItem(go)
    local flag = self:IsSelect(go.heroId)
    self:SetSelect(go.heroId,not flag)
    go:SetSelected(not flag)
    self:RefreshSelectedItem()
end
---@param go HeroBigIcon
function UIHeroSelect:ClickSelectedItem(go)
    if not go:IsEmpty() then
        self:SetSelect(go.heroId,false)
        self:RefreshSelectedItem()
        self.itemList:RefreshVirtualList()
    end
end
function UIHeroSelect:ClickBack()
    if #self.selectedList == 0 then
        logError("选择的卡牌数量不能为0")
        return
    end
    SetPlayerPrefs("SelectedCardList",getJsonStr(self.selectedList))
    closeUI(PanelNames.UIHeroSelect)
    showUI(PanelNames.UIBattleReady,self.selectedList)
end
function UIHeroSelect:ClickDeck()
    showUI(PanelNames.UICardDeck)
end
return UIHeroSelect